﻿using System;
using System.Linq;
using TtfLibrary.CommonStructures;

namespace TtfLibrary
{
    public class OttPost: IAfterRead, IAfterWrite
    {
        public TtfFixed Version;
        public TtfFixed ItalicAngle;
        public Int16 UnderlinePosition;
        public Int16 UnderlineThickness;
        public UInt32 IsFixedPitch;
        public UInt32 MinMemType42;
        public UInt32 MaxMemType42;
        public UInt32 MinMemType1;
        public UInt32 MaxMemType1;
        [TtfPartAttribute("NumberOfGlyphs")]
        public Int16 NumberOfGlyphs;
        [TtfPartAttribute("GlyphNameIndex")]
        public Int16[] GlyphNameIndex;
        [TtfPartAttribute("Names")]
        public string[] Names;

        public void AfterRead(TtfStructureReader reader, int startOffset)
        {
            if (this.Version.Major == 2 && this.Version.Minor == 0)
            {
                reader.Read(this, new[] { "NumberOfGlyphs" });
                this.GlyphNameIndex = new short[this.NumberOfGlyphs];
                reader.Read(this, new[] { "GlyphNameIndex" });

                var numberNewGlyphs = this.GlyphNameIndex.Max() - 258;
                if (numberNewGlyphs > 0)
                {
                    this.Names = new string[numberNewGlyphs];
                    reader.Read(this, new[] { "Names" });
                }
            }
        }

        public void AfterWrite(StructureWriter writer)
        {
            if (this.Version.Major == 2 && this.Version.Minor == 0)
            {
                writer.WriteObject(this, new[] { "NumberOfGlyphs" });
                writer.WriteObject(this, new[] { "GlyphNameIndex" });
                if (Names != null)
                    writer.WriteObject(this, new[] { "Names" });
            }
        }
    }
}
